﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Bussiness;

namespace EWebsite {
    public class CallWS {
        /// <summary>
        /// call ws login
        /// return SID
        /// </summary>
        /// <returns></returns>
        private static String wsLogin(String url, String serviceName, String name, String pwd) {
            object[] objArr = new object[] { name, pwd };
            object obj = WSProxy.CallWebService(url, serviceName, "WSLogin", objArr);

            return obj.ToString();
        }

#region "Broker ws"
        /// <summary>
        /// login broker
        /// return SID
        /// </summary>
        /// <returns></returns>
        public static String loginBroker() {
            BrokerBuss buss = new BrokerBuss();
            DataTable table = buss.getBroker();
            String loginID = table.Rows[0]["PvdID"].ToString().Trim();
            String loginPwd = table.Rows[0]["PvdPwd"].ToString().Trim();
            String url = table.Rows[0]["LinkWS"].ToString().Trim();
            String wsName = table.Rows[0]["WSName"].ToString().Trim();
            //txtCreditCardNumber.Text = table.Rows[0]["PvdCreditCardNumber"].ToString();

            return CallWS.wsLogin(url, wsName, loginID, loginPwd);
        }

        public static int tranferMoneyForProvider(String SID, String providerCreditCard, float amountMoney, String pvdCreditCardSecureNum = "", String pvdCreditCardHolderName = "") {
            BrokerBuss buss = new BrokerBuss();
            DataTable table = buss.getBroker();
            String url = table.Rows[0]["LinkWS"].ToString().Trim();
            String wsName = table.Rows[0]["WSName"].ToString().Trim();

            //object[] objArgs = new object[] {SID, providerCreditCard, amountMoney.ToString(), pvdCreditCardSecureNum, pvdCreditCardHolderName };
            object[] objArgs = new object[] { SID, providerCreditCard, amountMoney };
            object obj = WSProxy.CallWebService(url, wsName, "TranferMoneyForProvider", objArgs);

            return int.Parse(obj.ToString());
        }

        public static int tranferMoneyForProvider(String SID, String providerCreditCard, float amountMoney) {
            BrokerBuss buss = new BrokerBuss();
            DataTable table = buss.getBroker();
            String url = table.Rows[0]["LinkWS"].ToString().Trim();
            String wsName = table.Rows[0]["WSName"].ToString().Trim();

            object[] objArgs = new object[] { SID, providerCreditCard, amountMoney };
            object obj = WSProxy.CallWebService(url, wsName, "TranferMoneyForProvider", objArgs);

            return int.Parse(obj.ToString());
        }

#endregion


#region "partner"
        /// <summary>
        /// login partner ws
        /// return SID
        /// </summary>
        /// <returns></returns>
        public static String loginPartner(String url, String wsName, String name, String pwd) {
            return CallWS.wsLogin(url, wsName, name, pwd);
        }

        /// <summary>
        /// refuse transaction
        /// </summary>
        /// <param name="url">asmx file url</param>
        /// <param name="wsName"></param>
        /// <param name="SID"></param>
        /// <param name="transID"></param>
        /// <returns>0 : successful; -1 : fail; -2 : unknow error</returns>
        public static String refuseTrans(String url, String wsName, String SID, String transID) {
            object[] objArgs = new object[] {SID, transID };
            object obj = WSProxy.CallWebService(url, wsName, "RefuseTrans", objArgs);
            return obj.ToString();
        }

        /// <summary>
        /// accept transaction
        /// </summary>
        /// <param name="url">asmx file url</param>
        /// <param name="wsName">ws name</param>
        /// <param name="SID">sid</param>
        /// <param name="transID">transaction id</param>
        /// <returns>0 : successful; -1 : fail; -2 : unknow error</returns>
        public static String acceptTrans(String url, String wsName, String SID, String transID) {
            object[] objArgs = new object[] { SID, transID };
            object obj = WSProxy.CallWebService(url, wsName, "AcceptTrans", objArgs);
            return obj.ToString();
        }

        /// <summary>
        /// check inventory
        /// </summary>
        /// <param name="url">asmx file url</param>
        /// <param name="wsName">ws name</param>
        /// <param name="SID">session id</param>
        /// <param name="transID">transaction id</param>
        /// <returns>amount in partner's repository</returns>
        public static int checkInventory(String url, String wsName, String SID, String transID) {
            object[] objArgs = new object[] { SID, transID };
            object obj = WSProxy.CallWebService(url, wsName, "CheckInventory", objArgs);
            
            return int.Parse(obj.ToString());
        }

        /// <summary>
        /// update partner repository
        /// </summary>
        /// <param name="url"></param>
        /// <param name="wsName"></param>
        /// <param name="SID"></param>
        /// <param name="transID"></param>
        /// <param name="addedQuantity"></param>
        /// <param name="pricePerUnit"></param>
        /// <param name="transportPrice"></param>
        /// <param name="totalPrice"></param>
        /// <returns>0: success; -1: fail; -2: unknow error</returns>
        public static String updateInventory(String url, String wsName, String SID, String transID, int addedQuantity, float pricePerUnit, float transportPrice, float totalPrice) {
            object[] objArgs = new object[] { SID, transID, addedQuantity.ToString(), pricePerUnit.ToString(), transportPrice.ToString(), totalPrice.ToString() };
            object obj = WSProxy.CallWebService(url, wsName, "UpdateInventory", objArgs);

            return obj.ToString();
        }

        public static String updateInventory(String url, String wsName, String SID, String transID, int addedQuantity, float pricePerUnit, float transportPrice, float totalPrice, String linkTrans) {
            object[] objArgs = new object[] { SID, transID.Trim(), addedQuantity, pricePerUnit, transportPrice, totalPrice, linkTrans };
            object obj = WSProxy.CallWebService(url, wsName, "UpdateInventory", objArgs);

            return obj.ToString();
        }

#endregion

#region "carrier ws"
        /// <summary>
        /// login carrier
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pw"></param>
        /// <returns>session id</returns>
        public static String loginCarrier() {
           // String url = "http://aspspider.info/m15tmdt/WebService.asmx";
            //String wsName = "WebService";
            DataTable table = (new CarrierBuss()).getCarriers();
            String url = table.Rows[0]["LinkWS"].ToString().Trim();
            String wsName = table.Rows[0]["WSName"].ToString().Trim();
            String name = table.Rows[0]["PvdID"].ToString().Trim();
            String pwd = table.Rows[0]["pvdPwd"].ToString().Trim();

            return CallWS.wsLogin(url, wsName, name, pwd);
        }

        /// <summary>
        /// get cities list from carriers
        /// </summary>
        /// <returns></returns>
        public static DataTable citiesList() {
            DataTable table = new DataTable();

            table = (new CarrierBuss()).getCarriers();
            String url = table.Rows[0]["LinkWS"].ToString().Trim();
            String wsName = table.Rows[0]["WSName"].ToString().Trim();

            table = (DataTable)WSProxy.CallWebService(url, wsName, "CitiesList", new object[] { });
            return table;
        }

        /// <summary>
        /// cal price
        /// </summary>
        /// <param name="fromCity"></param>
        /// <param name="toCity"></param>
        /// <param name="demission"></param>
        /// <returns></returns>
        public static float routePrice(String fromCity, String toCity, string demission, int transportType) {
            //String url = "http://aspspider.info/m15tmdt/WebService.asmx";
            //String wsName = "WebService";
            DataTable table = (new CarrierBuss()).getCarriers();
            String url = table.Rows[0]["LinkWS"].ToString().Trim();
            String wsName = table.Rows[0]["WSName"].ToString().Trim();

            object[] objArgs = new object[] {fromCity, toCity, demission, transportType};
            object obj = WSProxy.CallWebService(url, wsName, "RoutePrice", objArgs);

            return float.Parse(obj.ToString());
        }

        /// <summary>
        /// place oders
        /// </summary>
        /// <param name="SID"></param>
        /// <param name="fromCity"></param>
        /// <param name="toCity"></param>
        /// <param name="dimession"></param>
        /// <param name="transportType"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public static String routePrice2(String SID, String fromCity, String toCity, int transportType, String dimession, DateTime date) {
            //String url = "http://aspspider.info/m15tmdt/WebService.asmx";
            //String wsName = "WebService";

            DataTable table = (new CarrierBuss()).getCarriers();
            String url = table.Rows[0]["LinkWS"].ToString();
            String wsName = table.Rows[0]["WSName"].ToString();

            //String temp = SID + "@" + fromCity + "@" + toCity + "@" + transportType.ToString() + "@" + dimession + "@" + "03/21/2012";
            object[] objArgs = new object[] { SID, fromCity, toCity, dimession, transportType, date };
            
            object obj = WSProxy.CallWebService(url, wsName, "RoutePrice2", objArgs);

            return obj.ToString();
        }
#endregion
    }
}